﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using Npgsql;
using System.Data;
using System.Xml;

namespace SpeedTestServerSetup
{
    public static class Help
    {

        public static void OpenHtmlHelp(int pageIndex)
        {
            OpenHtmlHelp(pageIndex.ToString() + ".html");
        }

        public static void OpenHtmlHelp(string helpFile)
        {
            string helpPath = Path.Combine(Application.StartupPath, "HelpFiles");
            string helpFilePath = Path.Combine(helpPath, helpFile);
            if (File.Exists(helpFilePath))
            {
                Process.Start(helpFilePath);
            }
        }
    }

	public class ConfigFile
	{
		private XmlDocument doc = new XmlDocument();
		private string _filePath = String.Empty;

		public ConfigFile(string filePath)
		{
			
			if(!String.IsNullOrEmpty(filePath))
			{
				StreamReader reader = new StreamReader(filePath);
				string configXml = reader.ReadToEnd();
				reader.Close();
				doc.LoadXml(configXml);
				_filePath = filePath;
			}
		}

		public void SetValue(string nodePath, string attribute, string nodeValue)
		{
			XmlNode node = doc.SelectSingleNode(nodePath);

			if (node != null)
			{		
				XmlAttribute attr = node.Attributes[attribute];
				attr.Value = nodeValue;
			}
		}

		public void Save()
		{
			doc.Save(_filePath);
		}

	}

	public static class ConnectionString
	{
		public static string Build(string serverName, string port, string userName, string password, string database)
		{
			string connString = "Server=" + serverName + ";";
			connString += "Port=" + port + ";";
			connString += "User Id=" + userName + ";";
			connString += "Password=" + password + ";";

            if (String.IsNullOrEmpty(database)) { connString += "Database=postgres;"; }
            else { connString += "Database=" + database + ";"; }

			return connString;
		}
	}

    public class Data
    {
        private NpgsqlConnection conn = null;
        private string connectionString = String.Empty;

        public Data(string serverName, string port, string userName, string password)
        {
            connectionString = ConnectionString.Build(serverName, port, userName, password, String.Empty);
        }

        public Data(string connectionString)
        {
            this.connectionString = connectionString;
        }

        public bool Disconnect()
        {
            if (conn != null & conn.State == ConnectionState.Open)
            {
                conn.Close();
                return true;
            }

            return false;
        }

        public bool Connect()
        {
            if (!String.IsNullOrEmpty(connectionString))
            {
                try
                {
                    conn = new NpgsqlConnection(connectionString);
                    conn.Open();
                }
                catch (Exception e)
                {
                    //TODO: log exception
                    return false;
                }
            }

            return true;
        }

        public bool PutData(string sqlStatement)
        {
            NpgsqlCommand cmd = null;

            if (conn != null & conn.State == ConnectionState.Open)
            {
                try
                {
                    cmd = conn.CreateCommand();
                    cmd.CommandText = sqlStatement;
                    cmd.ExecuteNonQuery();
                }
                catch(Exception e)
                {
                    //MessageBox.Show(e.Message);
                    return false;
                }
            }

            return true;
        }

        public bool PutDataFromFile(string filePath)
        {
            StreamReader stream = new StreamReader(filePath);
            string sqlStatement = stream.ReadToEnd();

            return PutData(sqlStatement);
        }
    }
}
